package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.ErrorBean;
import beans.UserBean;

public class MainDispatcher extends HttpServlet {

	//paths of the dispatchers
	private static String ProfileDispatcher = null;
	private static String ShopDispatcher = null;
	private static String CheckoutDispatcher = null;
	private static String AdminDispatcher = null;
	
	//modules name
	private static String MODULE_SHOP = null;
	private static String MODULE_PROFILE = null;
	private static String MODULE_CHECKOUT = null;
	private static String MODULE_ADMIN = null;

	// page, request, session, application variables
	private static final String ERROR = "errorMessage";
	private static final String USER_PROFILE = "user_profile";

	/**
	 * Constructor of the object.
	 */
	public MainDispatcher() {
		super();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		// Get the modules dispatcher.
		ProfileDispatcher = config.getInitParameter("PROFILE_DISPATCHER");
		ShopDispatcher = config.getInitParameter("SHOP_DISPATCHER");
		CheckoutDispatcher = config.getInitParameter("CHECKOUT_DISPATCHER");
		AdminDispatcher = config.getInitParameter("ADMIN_DISPATCHER");
		
		// Get the modules name
		MODULE_SHOP = config.getInitParameter("MODULE_SHOP");
		MODULE_PROFILE = config.getInitParameter("MODULE_PROFILE");
		MODULE_CHECKOUT = config.getInitParameter("MODULE_CHECKOUT");
		MODULE_ADMIN = config.getInitParameter("MODULE_ADMIN");
	}
	
	/**
	 * The processRequest method of the servlet. <br>
	 * 
	 * This method is called when a request is received
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String module = request.getParameter("mod");
		
		//module is not specified
		if(module == null){
			module=MODULE_SHOP;
		}
		//forward the request to the specific dispatcher.
		if(module.equals(MODULE_SHOP)){
			request.getRequestDispatcher(ShopDispatcher).forward(request, response);
		} else if(module.equals(MODULE_PROFILE)){
			request.getRequestDispatcher(ProfileDispatcher).forward(request, response);
		} else if(module.equals(MODULE_CHECKOUT)){
			request.getRequestDispatcher(CheckoutDispatcher).forward(request, response);
		} else if(module.equals(MODULE_ADMIN)){
			request.getRequestDispatcher(AdminDispatcher).forward(request, response);
		} else {
			request.getRequestDispatcher(ShopDispatcher).forward(request, response);
		}
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
}
